package com.aidong.carnival.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aidong.activity.dao.MstCampaignDao;
import com.aidong.activity.entity.MstCampaign;
import com.aidong.carnival.dao.InvitationDao;
import com.aidong.carnival.entity.Message;
import com.aidong.carnival.entity.TrnInvitationCode;
import com.aidong.carnival.service.InvitationService;
import com.aidong.jobs.constants.JobConst;
import com.aidong.pay.config.util.ClenderUtil;
import com.aidong.pay.dao.OrdergeneratedDao;
import com.aidong.util.bus.CodeConsts;

@Service
public class InvitationServiceImpl implements InvitationService {

	@Autowired
	InvitationDao invitationDao;
	
	@Autowired
	MstCampaignDao mstCampaignDao;
	
	@Autowired
	private OrdergeneratedDao ordergeneratedDao;

	@Override
	/**
	 * 根据邀请码判断该邀请码是否正确
	 * @param code
	 * @return
	 */
	public String checkInvitation(){
		return invitationDao.checkInvitation();
	}
	
	
	/**
	 * 保存该邀请码用户信息
	 * @param invitation
	 */
	@Override
	public void addInvitation(TrnInvitationCode invitation){
		invitationDao.addInvitation(invitation);
	}
	
	/**
	 * 获取邀请码活动ID
	 * @return
	 */
	@Override
	public MstCampaign getCampaignDetailByType(){
		Map<String, Object> payMap = new HashMap<String, Object>();
		payMap.put("typeCou", 13);
		return mstCampaignDao.getCarnivalByType(payMap);
	}
	
	public int getInvitationCounts(String code,int activitId,String phone){
		return invitationDao.getInvitationCounts(code, activitId,phone);
	}
	
	public int getCampaignByPhone(String phone,int activitId){
		return invitationDao.getCampaignByPhone(phone, activitId);
	}
	
	public int getInvitationByPhone(String code,String phone){
		return invitationDao.getInvitationByPhone(code, phone);
	}
	/**
	 * 添加邀请活动
	 *//*
	@Transactional(rollbackFor = { Exception.class })
	@Override
	public Message addinvitationBuy(String idongId,String contactName,String contactPhone,String phone)throws Exception {
		Message ms=new Message();
		if(!contactPhone.equals(phone)){
			ms.setCode(0);
			ms.setMessage("预约手机号与该用户绑定的手机号不一致");
			return ms;
		}
		int count=invitationDao.getInvitationCount(phone);
		if(count==0){
			ms.setCode(0);
			ms.setMessage("暂未收取到您的邀请码");
			return ms;
		}
		MstCampaign campaign=new MstCampaign();
		
		campaign.setIdongId(idongId); // 爱动号
		//int campaignId = campaign.getCampaignId(); // 活动ID
		// 解密联系人电话
		campaign.setContactPhone(contactPhone);
		campaign.setContactName(contactName);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sysDate = sdf.format(new Date());
		Date formatDate = sdf.parse(sysDate); // 系统当前年月日

		// 获得活动报名结束时间、报名人数、可参加人数
		Map<String, Object> msgMap = null;
		String signStratTime ="";  // 活动报名开始时间
		Date stratDateTime =null; 
		String signEndTime = ""; // 活动报名截止时间
		Date endDateTime =null; 
		int allowPerson = 0; // 活动可报名人数
		int signPerson = 0; // 活动报名有效人数（已报名人数）
		int version=0;
		int activitId=0;
		//需求变更后可取单条数据
		List<MstCampaign> list=mstCampaignDao.getCampaignAiDongList(13);
		if(list.size()==0){
			ms.setCode(0);
			ms.setMessage("无该活动邀请信息");
			return ms;
		}
		activitId=list.get(0).getCampaignId();
		
		if (activitId > 0 && (!"".equals(contactName) && contactName != null)
				&& (!"".equals(contactPhone) && contactPhone != null)) {
			
			Map<String,Object> paraMap=new HashMap<String,Object>();
			MstCampaign campaignDetail=null;
			msgMap = mstCampaignDao.getCampaignSignMsg(activitId);
			if(msgMap==null){
				ms.setCode(0);
				ms.setMessage("无该活动信息");
				return ms;
			}
			//根据爱动号和活动ID查看是否已预约过该活动
			paraMap=new HashMap<String, Object>();
			paraMap.put("campaignId",activitId);
			paraMap.put("idongId", idongId);
			List<Map<String, Object>> alreayMap = mstCampaignDao.getCarnivalIfAlreadyReg(paraMap);
			if (alreayMap.size() > 0) { // 是否已有报名记录
				for (Map<String, Object> alreay : alreayMap) {
					int alreayStatus = Integer.parseInt(alreay.get("status").toString());
					if (alreayStatus == CodeConsts.CODE_STATIC_BOOKING_BOOKING) { // 已预约状态
						ms.setCode(0);
						ms.setMessage("您已报名该活动，请到订单列表查看");
						return ms;
					}
				}
			}
			signStratTime = msgMap.get("stratSignTime").toString(); // 活动报名开始时间
			stratDateTime = sdf.parse(signStratTime);
			signEndTime = msgMap.get("endSignTime").toString(); // 活动报名截止时间
			endDateTime = sdf.parse(signEndTime);
			allowPerson = Integer.parseInt(msgMap.get("allowPerson").toString()); // 活动可报名人数
			signPerson = mstCampaignDao.getCampaignValidCount(activitId); // 活动报名有效人数（已报名人数）
			if(signPerson>=allowPerson){
				ms.setCode(0);
				ms.setMessage(msgMap.get("campaignTitle").toString()+"报名人数已满");
				return ms;
			}
			if(formatDate.compareTo(endDateTime) == 1 || formatDate.compareTo(stratDateTime) == -1){
				ms.setCode(0);
				ms.setMessage("活动："+msgMap.get("campaignTitle").toString()+"不在活动报名时间范围之内");
				return ms;
			}
			
			//根据活动ID获取该活动是否预约已满
			paraMap=new HashMap<String, Object>();
			paraMap.put("campaignId",activitId);
			campaignDetail=mstCampaignDao.getFullByCampaignId(paraMap);
			if(campaignDetail==null){
				ms.setCode(0);
				ms.setMessage("无该活动");
				return ms;
			}
			if(Integer.parseInt(campaignDetail.getIsFull())==1){
				ms.setCode(0);
				ms.setMessage(campaignDetail.getCampaignTitle()+"报名人数已满");
				return ms;
			}
			msgMap =null;
			msgMap = mstCampaignDao.getCampaignSignMsg(activitId);
			signStratTime = msgMap.get("stratSignTime").toString(); // 活动报名开始时间
			stratDateTime = sdf.parse(signStratTime);
			signEndTime = msgMap.get("endSignTime").toString(); // 活动报名截止时间
			endDateTime = sdf.parse(signEndTime);
			//brandId = Integer.valueOf(msgMap.get("brandId").toString());
			allowPerson = Integer.parseInt(msgMap.get("allowPerson").toString()); // 活动可报名人数
			signPerson = mstCampaignDao.getCampaignValidCount(activitId); // 活动报名有效人数（已报名人数）
			version=Integer.parseInt(msgMap.get("version").toString());
			*//**
			 * 查询该活动是否已报满
			 *//*
			paraMap=new HashMap<String,Object>();
			paraMap.put("campaignId",activitId);
			paraMap.put("version", version);
			campaignDetail=mstCampaignDao.getFullByCampaignId(paraMap);
			if(campaignDetail ==null || Integer.parseInt(campaignDetail.getIsFull())==1){
				throw new Exception();
			}else{ 
				paraMap.put("signPerson", signPerson);
				paraMap.put("allowPerson", allowPerson);
				int result=mstCampaignDao.updateFullByCampaignId(paraMap);
				if(result==0){
					throw new UnsupportedOperationException();
				}
			}
			
			campaign.setMovieFlg(0);
			campaign.setCampaignId(activitId);
			
			double price=0.00;
			Map<String, Object> payMap = new HashMap<String, Object>();
			payMap.put("typeCou", 13);
			campaignDetail=mstCampaignDao.getCarnivalByType(payMap);
			campaign.setCampaignId(campaignDetail.getCampaignId());//将嘉年华总的活动
			price=campaignDetail.getCamPrice();
			String title= campaignDetail.getCampaignTitle();
			// 添加预约信息
			mstCampaignDao.addActivityReg(campaign);
			int subscribeId = campaign.getSubscribeId(); // 预约ID
			// 添加付款信息
			payMap=new HashMap<String, Object>();
			payMap.put("subscribeId", subscribeId);
			payMap.put("orderType", 1);
			payMap.put("amount",price);
			payMap.put("title",title);
			payMap.put("idongId", campaign.getIdongId());
			payMap.put("nickName", campaign.getContactName());
			payMap.put("phone", campaign.getContactPhone());
			
			payMap.put("status",1);
			payMap.put("processId",2);
			payMap.put("tickNo",ClenderUtil.electronicTicketNo(campaign.getContactPhone())); // 电子券号
			
			
			ordergeneratedDao.addCarnivalCampaginOrderPay(payMap);
			String orderId = payMap.get("orderId").toString();
			ms.setCode(0);
			ms.setMessage("恭喜您，预约已成功");
			return ms;
			Map<String,Object> map=new HashMap<String, Object>();	
			map.put("subscribeId", subscribeId);
			map.put("orderId", orderId);
			map.put("zhifubao", true);
			map.put("weixin", true);
			map.put("voucher", false);
			map.put("exchange", false);
			map.put("membership", false);
			map.put("discount", false);
			map.put("price", price);
			map.put("campaignId", campaignDetail.getCampaignId());
			map.put("CampaignStartTime", campaignDetail.getCampaignTime());
			map.put("title", title);
			ms.setCode(1);
			ms.setMap(map);
			return ms;
		} else {
			ms.setCode(0);
			ms.setMessage("参数缺失");
			return ms;
		}
	}*/
	
	
}
